import requests
from lxml import etree
import execjs


def first_request():
    response = session.get(url, headers=headers)
    obj_html = etree.HTML(response.text)
    content_data = obj_html.xpath('//meta[2]/@content')[0]
    ts_url = 'http://iservice.10010.com' + obj_html.xpath('//script[1]/@src')[0]
    func_code = obj_html.xpath('//script[2]/text()')[0]

    response = session.get(ts_url, headers=headers)
    ts_code = response.text
    return content_data, ts_code, func_code


def second_request():
    content_data, ts_code, func_code = first_request()
    with open('main.js', encoding='utf-8') as f:
        code = f.read().replace("'func_code'", func_code).replace('content_code', content_data).replace("'ts_code'", ts_code)

    # print(code)
    cookie = execjs.compile(code).call('get_cookie')
    cookie = cookie.split(';')[0].split('=')
    print(cookie)
    session.cookies[cookie[0]] = cookie[1]
    response = session.get(url, headers=headers)
    response.encoding = response.apparent_encoding
    print(response)
    print(response.text)




if __name__ == '__main__':
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Pragma": "no-cache",
        "Referer": "http://iservice.10010.com/e5/query.html",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
    }
    url = "http://iservice.10010.com/e5/query.html"
    session = requests.session()
    second_request()
